package com.yolo.controller;


import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yolo.common.annotation.Log;
import com.yolo.common.enums.BusinessType;
import com.yolo.entity.Post;
import com.yolo.entity.dto.DeptPostView;
import com.yolo.service.DeptPostViewService;
import com.yolo.service.PostService;
import com.yolo.util.Result;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 岗位信息
 * </p>
 *
 * @author 霜蟾
 * @since 2021-04-12
 */
@RestController
@RequestMapping("/post")
@CrossOrigin
public class PostController {
    @Autowired
    private PostService postService;

    @Autowired
    private DeptPostViewService deptPostViewService;

    @Log(title = "岗位模块",businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public Result addPost(@RequestBody Post post) {
        postService.save(post);
        return Result.succ(null);
    }

    @Log(title = "岗位模块",businessType = BusinessType.DELETE)
    @DeleteMapping("/delete/{postId}")
    public Result deletePost(@PathVariable("postId") int postId) {
        postService.removeById(postId);
        return Result.succ(null);
    }

    @Log(title = "岗位模块",businessType = BusinessType.DELETE)
    @DeleteMapping("/batchDelete/{postIds}")
    public Result deletePost(@PathVariable("postIds") String postIds) {
        String[] split = postIds.split(",");
        postService.removeByIds(Arrays.asList(split));
        return Result.succ(null);
    }

    @Log(title = "岗位模块",businessType = BusinessType.QUERY)
    @GetMapping("/get/{postId}")
    public Result getPost(@PathVariable("postId") int postId) {
        Post post = postService.getById(postId);
        return Result.succ(post);
    }

    @Log(title = "岗位模块",businessType = BusinessType.UPDATE)
    @PutMapping("/update")
    public Result updatePost(@RequestBody Post post) {
        postService.updateById(post);
        return Result.succ(null);
    }

    @Log(title = "岗位模块",businessType = BusinessType.QUERY)
    @GetMapping("/list")
    public Result selectPagePost(int pageIndex, int pageSize, String postName, String postId) {
        Page<DeptPostView> page = new Page<>(pageIndex, pageSize);
        QueryWrapper qw = new QueryWrapper();
        if (postId != null && !postId.equals("")) {
            qw.eq("post_id", Integer.parseInt(postId));
        }
        qw.like("post_name", postName);
        Page<DeptPostView> page1 = deptPostViewService.page(page, qw);
        return Result.succ(MapUtil.builder()
                .put("count", page1.getTotal())
                .put("data", page1.getRecords()).map());
    }

    @Log(title = "岗位模块",businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public void exportPost(HttpServletResponse response) {
        try {
            List<DeptPostView> list = deptPostViewService.list();
            @SuppressWarnings("resource")
            HSSFWorkbook wb = new HSSFWorkbook();
//            HSSFCellStyle style = ExcelStyleUtil.getStyle(wb);
            HSSFSheet sheet = wb.createSheet("记录");
            HSSFRow row ;
            int columnIndex = 0;
            row = sheet.createRow(0);
            row.setHeight((short) (22.50 * 10));//设置行高
            row.createCell(columnIndex).setCellValue("序号");
            row.createCell(++columnIndex).setCellValue("岗位编号");
            row.createCell(++columnIndex).setCellValue("岗位名称");
            row.createCell(++columnIndex).setCellValue("岗位底薪");
            row.createCell(++columnIndex).setCellValue("岗位所属部门");
            for (int i = 0; i < list.size(); i++) {
                row = sheet.createRow(i + 1);
                DeptPostView entity = list.get(i);
                columnIndex = 0;
                row.createCell(columnIndex).setCellValue(i + 1);
                row.createCell(++columnIndex).setCellValue(entity.getPostId());
                row.createCell(++columnIndex).setCellValue(entity.getPostName());
                row.createCell(++columnIndex).setCellValue(entity.getSalary().toString());
                row.createCell(++columnIndex).setCellValue(entity.getDeptName());
            }
            sheet.setDefaultRowHeight((short) (16.5 * 20));
            //列宽自适应
            for (int i = 0; i <= 11; i++) {
                sheet.autoSizeColumn(i);
            }
            String title= "czjl_all";
            response.setHeader("Content-disposition", "attachment;fileName=" + title + ".xls");
            response.setContentType("application/octet-stream;charset=utf-8");
            OutputStream ouputStream = response.getOutputStream();
            wb.write(ouputStream);
            ouputStream.flush();
            ouputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
